//
//  SELeftController.m
//  kuaiyi
//
//  Created by Alphts on 16/7/5.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SELeftController.h"
#import "SEAboutController.h"
#import "SECaseManagerController.h"
#import "SEDoctorApplyController.h"
#import "SEFocusController.h"
#import "SESettingController.h"
#import "SEShareController.h"
#import "SELoginController.h"
#import "SERegistController.h"
#import "SEusrInfoController.h"



#define kUserImageViewWidth 70
#define kUserName @"username"
@interface SELeftController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>

//登录界面头部视图
@property (nonatomic, weak) UIView *loginBGView;

//未登录界面头部视图
@property (nonatomic, weak) UIView *unLoginBGView;

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArray1;
@property (nonatomic, strong) NSArray *dataArray2;
//用户名
@property (nonatomic, weak) UIView *topView;

@property(nonatomic,strong) UIViewController *usrInfoVC;

@end

@implementation SELeftController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view reloadInputViews];
    
    self.dataArray1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.dataArray2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];
    
    [self setupUI];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"leftCell"];
}



#pragma mark - 设置UI界面
- (void)setupUI{
    
    //读取用户偏好中的值 判断加载左侧的视图
    //思路--->读取用户偏好中的用户名--->有:登录界面;无:未登录界面
    //思路: 第一次进来 进入未登录界面,登录成功后跳转到Home控制器,再返回到登录好的界面
    NSString *username = [MLDefaults objectForKey:kUserName];
    NSString *password = [MLDefaults objectForKey:@"password"];
    
    if ([username isEqualToString:@"37"] && [password isEqualToString:@"37"]) {
        //有用户名的直接登录
        
        [self setupLoginBGView];
        self.topView = self.loginBGView;
        
    }else{
        //没有用户名的进入未登录界面
        [self setupUnLoginBGview];
        
        self.topView = self.unLoginBGView;
        
    }
    
    [self setupTableView];
    
}


#pragma mark - 加载tableview
- (void)setupTableView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.leading.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_LEFT_WIDTH);
    }];
    
    
}

#pragma mark - 未登录界面顶部的view
- (void)setupUnLoginBGview{
    
    //MARK: 创建控件
    //头部大view
    UIView *unLoginBGView = [[UIView alloc] init];
    self.unLoginBGView = unLoginBGView;
    unLoginBGView.backgroundColor = kThemeColor;
    [self.view addSubview:unLoginBGView];
    
    //用户待登录头像
    UIImageView *iv_user = [[UIImageView alloc] init];
    iv_user.image = [UIImage imageNamed:@"name"];
    iv_user.bounds = CGRectMake(0, 0, 50, 50);
    iv_user.layer.cornerRadius = iv_user.w / 2;
    iv_user.layer.masksToBounds = YES;
    [unLoginBGView addSubview:iv_user];
    
    //登录btn
    UIButton *bt_login = [UIButton buttonWithType:UIButtonTypeCustom];
    [bt_login setTitle:@"登录" forState:UIControlStateNormal];
    bt_login.titleLabel.font = FONT(18);
    [bt_login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    bt_login.tag = 101;
    [bt_login addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [unLoginBGView addSubview:bt_login];
    
    
    //注册btn
    UIButton *bt_regist = [UIButton buttonWithType:UIButtonTypeCustom];
    [bt_regist setTitle:@"注册" forState:UIControlStateNormal];
    bt_regist.titleLabel.font = FONT(18);
    [bt_regist setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    bt_regist.tag = 102;
    [bt_regist addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [unLoginBGView addSubview:bt_regist];
    
    //MARK: 设置约束
    //头部大view
    [unLoginBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top);
        make.leading.mas_equalTo(self.view);
        make.width.mas_equalTo(SCREEN_LEFT_WIDTH);
        make.height.mas_equalTo(110);
        
    }];
    
    //用户待登录头像
    [iv_user mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(unLoginBGView);
        make.top.mas_equalTo(self.view.mas_top).offset(20);
        make.width.height.mas_equalTo(iv_user.w);
        
    }];
    
    //登录btn
    [bt_login mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(self.unLoginBGView.centerX).offset(-10);
        make.bottom.equalTo(self.unLoginBGView).offset(-10);
        make.size.equalTo(CGSizeMake(100, 30));
        
    }];
    
    //注册btn
    [bt_regist mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.unLoginBGView.centerX).offset(10);
        make.size.equalTo(CGSizeMake(100, 30));
        make.bottom.equalTo(self.unLoginBGView).offset(-10);
        
    }];
    
}


#pragma mark - 已登录界面的顶部view
- (void)setupLoginBGView{
    //MARK: 创建控件
    //头部view
    UIView *loginBGView = [[UIView alloc] init];
    self.loginBGView = loginBGView;
    self.loginBGView.backgroundColor = kThemeColor;
    [self.view addSubview:_loginBGView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(usrInfo:)];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(usrInfo:)];
    
    
    tap.delegate = self;
    
    //用户头像
    UIImageView *iv_user = [[UIImageView alloc] init];
    iv_user.image = [UIImage imageNamed:@"shinji"];
    iv_user.layer.cornerRadius = kUserImageViewWidth / 2;
    iv_user.layer.masksToBounds = YES;
    
    iv_user.userInteractionEnabled = YES;
    [iv_user addGestureRecognizer:tap2];
    [_loginBGView addSubview:iv_user];
    
    
    
    //用户资料label
    UILabel *lb_userInfo = [[UILabel alloc] init];
    lb_userInfo.text = @"查看个人资料";
    lb_userInfo.font = FONT(15);
    
    lb_userInfo.userInteractionEnabled =YES;
    [lb_userInfo addGestureRecognizer:tap];
    [_loginBGView addSubview:lb_userInfo];
    
    //用户姓名label
    UILabel *lb_username = [[UILabel alloc] init];
    lb_username.text = @"碇シンジ";
    lb_username.font = FONT(18);
    lb_username.textColor = [UIColor blackColor];
    [_loginBGView addSubview:lb_username];
    
    //MARK: 设置约束
    // 头部view
    [_loginBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top);
        
        make.leading.trailing.mas_equalTo(self.view);
        
        make.height.mas_equalTo(110);
    }];
    
    //用户头像
    [iv_user mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.width.mas_equalTo(kUserImageViewWidth);
        
        make.leading.mas_equalTo(self.view).offset(20);
        
        make.centerY.mas_equalTo(_loginBGView).offset(10);
        
    }];
    
    //用户姓名label
    [lb_username mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.mas_equalTo(iv_user.mas_trailing).offset(20);
        
        make.top.mas_equalTo(_loginBGView.mas_top).offset(35);
        
    }];
    
    //用户资料label
    [lb_userInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(lb_username);
        
        make.top.equalTo(lb_username.mas_bottom).offset(16);
        
    }];
    
    
}

#pragma mark - 私有方法
//点击登录或者注册按钮
- (void)didClickButton:(UIButton *)sender{
    
    SEBaseController *vc;
    
    if (sender.tag == 101) {
        vc = [[SELoginController alloc] init];
        
        SELoginController *loginVc = (SELoginController *)vc;
        
        //调用Block,显示登录完毕后的已登录界面
        loginVc.returnBlk = ^(){
            
            self.topView = self.loginBGView;
            [self setupLoginBGView];
        };
        
    }else{
        vc = [[SERegistController alloc] init];
    }
    
    //每次都需要跳转到
    [self jumpToOtherViewController:vc];
    
}


#pragma mark - 数据源和代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell" forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        cell.textLabel.text = self.dataArray1[indexPath.row];
    }else{
        cell.textLabel.text = self.dataArray2[indexPath.row];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SEBaseController *vc;
    
    if (indexPath.section == 0) {
        
        switch (indexPath.row) {
            case 0:
                vc = [[SEDoctorApplyController alloc] init];
                break;
            case 1:
                vc = [[SECaseManagerController alloc] init];
                break;
            case 2:
                vc = [[SEFocusController alloc] init];
                break;
                
            default:
                break;
        }
        
        vc.title = self.dataArray1[indexPath.row];
        
    }else{
        switch (indexPath.row) {
            case 0:
                vc = [[SESettingController alloc] init];
                
            {SESettingController *setVC = (SESettingController *)vc;
                setVC.returnBackBlock = ^{
                    
                    [self setupUnLoginBGview];
                    
                    self.topView = self.unLoginBGView;
                };}
                
                break;
                
            case 1:
                vc = [[SEAboutController alloc] init];
                break;
                
            case 2:
                vc = [[SEShareController alloc] init];
                break;
                
            default:
                break;
        }
        vc.title = self.dataArray2[indexPath.row];
    }
    
    //通过JASidePanel跳转
    [self jumpToOtherViewController:vc];
    
}

- (void)usrInfo:(UIGestureRecognizer*)tap{
    
    _usrInfoVC = [SEusrInfoController new];
    
    [self jumpToOtherViewController:_usrInfoVC];
    
    
}

@end
